package com.rentong.wx.cust.service.impl;

import com.rentong.page.Page;
import com.rentong.wx.cust.dao.CustMessageChatLogDao;
import com.rentong.wx.cust.dao.CustMessageChatTopDao;
import com.rentong.wx.cust.dao.WxCustUserMapper;
import com.rentong.wx.cust.domain.CustMessageChatLogEntity;
import com.rentong.wx.cust.domain.WxCustUser;
import com.rentong.wx.cust.dto.*;

import com.rentong.wx.cust.service.CustMessageChatLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustMessageChatLogServiceImpl implements CustMessageChatLogService {

    @Resource
    private CustMessageChatLogDao custMessageChatLogDao;

    @Resource
    private CustMessageChatTopDao custMessageChatTopDao;

    @Autowired
    private WxCustUserMapper wxCustUserMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertMessageByCustId(CustMessageChatLogEntity custMessageChatLogEntity) {
        custMessageChatLogEntity.setAddTime(new Date());
        custMessageChatLogDao.insertSelective(custMessageChatLogEntity);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Page getChatList(Long custId, Integer totalNum, Integer weidu) {
        Page page = new Page();
        List<CustMessageChatDto> chatListNew = new ArrayList<>();

        //先查询下，未读消息的总数是否一样，一样的话，就不查询后续了
        Integer weiduZongshu = custMessageChatLogDao.getChatWeiduZongshu(custId);
        if (totalNum.toString().equals(weiduZongshu.toString())) {
            page.setTotalNum(-1);
            return page;
        }


        List<CustMessageChatDto> chatList = custMessageChatLogDao.getChatList(custId);

        Integer count = 0;
        for (int i = 0; i < chatList.size(); i++) {
            CustMessageChatDto cmc = chatList.get(i);
            CustMessageChatDto custMessageChatDto = custMessageChatLogDao.getChatListById(cmc.getCId(), custId);

            if (weidu == 1 && custMessageChatDto.getCountNum() == 0) {
                //查询未读的，并且没有未读的
                continue;
            }

            //把数据填充下，更新下
            chatList.get(i).setMessageType(custMessageChatDto.getMessageType());
            chatList.get(i).setMessageContent(custMessageChatDto.getMessageContent());
            chatList.get(i).setAddTime(custMessageChatDto.getAddTime());
            chatList.get(i).setCountNum(custMessageChatDto.getCountNum());

            //累计下有多少未读的
            count += custMessageChatDto.getCountNum();

            chatListNew.add(chatList.get(i));
        }

        page.setTotalNum(count);
        page.setResultList(chatListNew);

        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<CustMessageChatLogEntityDto> getChatContent(Long custId, Long userId) {
        //把未读的变成已读
        custMessageChatLogDao.updateMessagetState(custId, userId);
        WxCustUser wxCustUser = wxCustUserMapper.selectByPrimaryKey(userId);
        //再查询数据
        List<CustMessageChatLogEntityDto> chatContent = custMessageChatLogDao.getChatContent(userId, custId);
        for (CustMessageChatLogEntityDto custMessageChatLogEntityDto : chatContent) {
            if (custMessageChatLogEntityDto.getSenderId() == userId) {
                custMessageChatLogEntityDto.setReceiverAvatarId(wxCustUser.getAvatarId());
            }
        }
        return chatContent;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateReadeStatus(Long custId, Long userId) {
        //查出最后一条是未读还是已读
        Integer notReadNum = custMessageChatLogDao.getNotRead(custId, userId);

        if (notReadNum > 0) {
            //有未读的
            //全部标记为已读
            custMessageChatLogDao.updateMessagetState(userId, custId);

            //把最后一条标记为未读
            return custMessageChatLogDao.updateReadeStatus(custId, userId);

        } else {
            //没有未读的
            //把最后一条标记为未读
            return custMessageChatLogDao.updateReadeStatus(custId, userId);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delInformation(Long custId, Long userId) {
        //先删除聊天记录
        Integer integer1 = custMessageChatLogDao.delInformation(custId, userId);
        Integer integer2 = custMessageChatLogDao.delInformation2(custId, userId);

        //再删除置顶的
        custMessageChatTopDao.delTopByUserId(custId, userId);

        return integer1 + integer2;
    }


}